Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
debakarr
GitHub Repository: debakarr/machinelearning
Path: blob/master/Part 9 - Dimension Reduction/Principal Component Analysis/[R] Principal Component Analysis.ipynb
1341 views
Kernel: R

Principal Component Analysis

Data preprocessing

# Import the dataset dataset = read.csv('Wine.csv')
head(dataset, 10)
# Splitting the dataset into the Training set and Test set library(caTools) set.seed(42) split = sample.split(dataset$Customer_Segment, SplitRatio = 0.8) training_set = subset(dataset, split == TRUE) test_set = subset(dataset, split == FALSE)
head(training_set, 10)
head(test_set, 10)
# Feature Scaling training_set[-14] = scale(training_set[-14]) test_set[-14] = scale(test_set[-14])
head(training_set, 10)
head(test_set, 10)

Applying Principal Component Analysis

library(caret) library(e1071)
Loading required package: lattice Loading required package: ggplot2
pca = preProcess(x = training_set[-14], method = 'pca', pcaComp = 2) training_set = predict(pca, training_set)
head(training_set, 10)
training_set = training_set[c(2, 3, 1)] # Reordering the columns
head(training_set, 10)
test_set = predict(pca, test_set) test_set = test_set[c(2, 3, 1)] # Reordering the columns
head(test_set, 10)

Fitting classifier to the Training set

classifier = svm(formula = Customer_Segment ~ ., data = training_set, type = 'C-classification', kernel = 'radial')

Predicting the Test set results

y_pred = predict(classifier, newdata = test_set[-3])
head(y_pred, 10)
head(test_set[3], 10)

Making the Confusion Matrix

cm = table(test_set[, 3], y_pred)
cm
y_pred 1 2 3 1 11 1 0 2 0 14 0 3 0 0 10

classifier made 11 + 14 + 10 = 35 correct prediction and 1 incoreect prediction.


Visualizing the Training set results

# install.packages('ElemStatLearn') library(ElemStatLearn)
set = training_set
X1 = seq(min(set[, 1]) - 1, max(set[, 1]) + 1, by = 0.01) X2 = seq(min(set[, 2]) - 1, max(set[, 2]) + 1, by = 0.01) grid_set = expand.grid(X1, X2) colnames(grid_set) = c('PC1', 'PC2') y_grid = predict(classifier, newdata = grid_set) plot(set[, -3], main = 'Kernel SVM (Training set)', xlab = '1st Principal Component', ylab = '2nd Principal Component', xlim = range(X1), ylim = range(X2)) contour(X1, X2, matrix(as.numeric(y_grid), length(X1), length(X2)), add = TRUE) points(grid_set, pch = '.', col = ifelse(y_grid == 2, 'lightblue', ifelse(y_grid == 1, 'springgreen3', 'tomato'))) points(set, pch = 21, bg = ifelse(set[, 3] == 2, 'blue3', ifelse(set[, 3] == 1, 'green4', 'red3')), col='white') legend("topright", legend = c("0", "1", "2"), pch = 16, col = c('red3', 'green4', 'blue3'))
Image in a Jupyter notebook

Visualizing the Test set results

set = test_set
X1 = seq(min(set[, 1]) - 1, max(set[, 1]) + 1, by = 0.01) X2 = seq(min(set[, 2]) - 1, max(set[, 2]) + 1, by = 0.01) grid_set = expand.grid(X1, X2) colnames(grid_set) = c('PC1', 'PC2') y_grid = predict(classifier, newdata = grid_set) plot(set[, -3], main = 'Kernel SVM (Test set)', xlab = '1st Principal Component', ylab = '2nd Principal Component', xlim = range(X1), ylim = range(X2)) contour(X1, X2, matrix(as.numeric(y_grid), length(X1), length(X2)), add = TRUE) points(grid_set, pch = '.', col = ifelse(y_grid == 2, 'lightblue', ifelse(y_grid == 1, 'springgreen3', 'tomato'))) points(set, pch = 21, bg = ifelse(set[, 3] == 2, 'blue3', ifelse(set[, 3] == 1, 'green4', 'red3')), col='white') legend("topright", legend = c("0", "1", "2"), pch = 16, col = c('red3', 'green4', 'blue3'))
Image in a Jupyter notebook